# -*- coding: utf-8 -*
import os
import requests
import json
from flask import Flask, request, render_template, Markup

app = Flask(__name__)
ip = "http://test-mars.dev-bp.atcloudbox.com"
headers = {'X-Platform': 'Android', 'Content-Type': 'application/json', 'charset':'utf-8',
    "X-Version-Code": "1160", "X-TVU-Code": "0", "User-Agent":"okhttp/3.12.0",
    "Accept-Encoding": "gzip"}


# @app.route('/', methods=['GET', 'POST'])
# def route_map():
#     rules_iterator = app.url_map.iter_rules()
#     return json.dumps({rule.endpoint: rule.rule for rule in rules_iterator})


@app.route('/', methods=['GET', 'POST'])
#重置新用户
def reset():
    if request.method == 'GET':
        return render_template('index.html', res_text='')
    else:
        x_dog_token1 = request.form.get('x_dog_token1')
        headers['X-Dog-Token'] = x_dog_token1
        res = requests.post(ip + '/game/reset_device_id', headers=headers)
        res = res.text
        user = json.loads(res)
        if user.get('code') == 0:
            res = '成功重置为新用户'
        else:
            res = '该用户不存在'
    return render_template('index.html', x_dog_token1=x_dog_token1, res_text=res)

#增加主线关卡
@app.route('/', methods=['GET', 'POST'])
def test_add_user_checkpoint():
    if request.method == 'GET':
        return render_template('index.html', checkpoint='')
    else:
        x_dog_token2 = request.form.get('x_dog_token2')
        checkpoint = int(request.form.get('checkpoint'))
        print(x_dog_token2)
        print(checkpoint)
        body = {
            "checkpoint": checkpoint
        }

        headers['X-Dog-Token'] = x_dog_token2
        checkpoint_res = requests.post(ip + '/game/reset', headers=headers, data=json.dumps(body))
        print(checkpoint_res.text)
        checkpoint_res = checkpoint_res.text
        user = json.loads(checkpoint_res)
        if user.get('code') == 50104:
            checkpoint_res = 'token不存在，增加主线关卡失败'
        else:
            checkpoint_res = str(x_dog_token2) + '该账号成功到达第' + str(checkpoint+1) + '关'

        return render_template('index.html', x_dog_token2=x_dog_token2, checkpoint_text=checkpoint_res)







if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)
